<?php

namespace App\Service;

use Illuminate\Support\Facades\Log;

/**
 * shell脚本.
 */
class ScriptService
{
    /*脚本目录*/
    const SCRIPT_PATH = '/var/www/html/dragon_op_bg/script/';
    const SCRIPT_BASE_PATH = '/server/script/';

    /**
     * [openServer 执行开服脚本].
     *
     * @Author   王文凡
     * @DateTime 2018-03-24
     *
     * @version  1.0
     *
     * @param int $server_id [服务器id]
     * @param int $port      [服务器端口]
     */
    public static function openServer($server_id, $port)
    {
        self::execute('sudo '.self::SCRIPT_PATH.'openserver_auto.sh '.
            intval($server_id).' '.intval($port).' '.env('DB_USERNAME').' '.
            env('DB_PASSWORD'));
    }

    /**
     * [stopServer 关服脚本].
     *
     * @Author   王文凡
     * @DateTime 2018-04-03
     *
     * @version  1.0
     *
     * @param int $server_id [服务器id]
     *
     * @return bool
     */
    public static function stopServer($server_id)
    {
        self::execute('cd '.self::SCRIPT_BASE_PATH.';sudo '.self::SCRIPT_BASE_PATH.'stop_gs_auto.sh gs_'.$server_id);

        return true;
    }

    /**
     * [mergeWeapon 合服回收武器].
     *
     * @Author   王文凡
     * @DateTime 2018-04-03
     *
     * @version  1.0
     *
     * @param int $server_id [服务器id]
     *
     * @return bool
     */
    public static function mergeWeapon($server_id)
    {
        self::execute('cd '.self::SCRIPT_BASE_PATH.';sudo '.self::SCRIPT_BASE_PATH.'merge_redraw.sh gs_'.$server_id);

        return true;
    }

    /**
     * [startGame 执行游戏启动脚本].
     *
     * @Author   王文凡
     * @DateTime 2018-03-26
     *
     * @version  1.0
     *
     * @param int $server_id [游戏服id]
     */
    public static function startGame($server_id)
    {
        self::execute('cd '.self::SCRIPT_BASE_PATH.';sudo '.self::SCRIPT_BASE_PATH.
            'start_gs.sh'.' '.$server_id);
    }

    /**
     * [exePlanMerge 执行合服配置文件脚本].
     *
     * @Author   王文凡
     * @DateTime 2018-03-26
     *
     * @version  1.0
     *
     * @param int    $server_id [服务器id]
     * @param string $merge_ids [待合服id]
     */
    public static function exePlanMerge($server_id, $merge_ids)
    {
        self::execute('sudo'.self::SCRIPT_PATH.'ch_merge_config.sh '.
            $server_id.' '.$merge_ids);
    }

    /**
     * [exeMerge 执行合服脚本].
     *
     * @Author   王文凡
     * @DateTime 2018-03-09
     *
     * @version  1.0
     *
     * @param int    $from_id [被合服的id]
     * @param int    $to_id   [合服id]
     * @param string $param   [参数]
     */
    public static function exeMerge($from_id, $to_id, $param)
    {
        self::execute('sudo '.self::SCRIPT_BASE_PATH.'merge_auto.sh '.$from_id
            .' '.$to_id.' '.$param);
    }

    /**
     * [grepServer 查询服务器id进程].
     *
     * @Author   王文凡
     * @DateTime 2018-03-09
     *
     * @version  1.0
     *
     * @param string $id [服务器id]
     *
     * @return bool|int
     */
    public static function grepServer($id = '')
    {
        if (!$id) {
            return false;
        }
        $run = self::execute('sudo ps -ef | grep gs_'.$id.'[[:space:]] | grep -v grep | wc -l');

        return trim($run);
    }

    /**
     * [mergeServer 查看合服进程].
     *
     * @Author   王文凡
     * @DateTime 2018-03-09
     *
     * @version  1.0
     *
     * @return int
     */
    public static function mergeServer()
    {
        $run = self::execute('sudo ps -ef | grep merge | grep -v grep | wc -l');

        return trim($run);
    }

    /**
     * [execute 执行脚本].
     *
     * @Author   王文凡
     * @DateTime 2018-03-09
     *
     * @version  1.0
     *
     * @param string $script
     *
     * @return bool|string
     */
    public static function execute($script = '')
    {
        if (!$script) {
            return false;
        }
        Log::info('shell script execute record:', ['script' => $script]);
        $result = shell_exec($script);
        Log::info('shell script execute result:', ['result' => $result]);

        return $result;
    }
}
